<template>
  <view class="space-query-page">
    <!-- 顶部搜索区域 -->
    <view class="search-section">
      <u-search
        v-model="searchKeyword"
        placeholder="搜索停车场或车位"
        :show-action="true"
        action-text="搜索"
        @search="handleSearch"
        @custom="handleSearch"
      />
    </view>

    <!-- 筛选条件 -->
    <view class="filter-section">
      <view class="filter-row">
        <u-button 
          size="mini" 
          :type="filterType === 'all' ? 'primary' : 'info'"
          @click="setFilterType('all')"
        >
          全部
        </u-button>
        <u-button 
          size="mini" 
          :type="filterType === 'available' ? 'primary' : 'info'"
          @click="setFilterType('available')"
        >
          有空位
        </u-button>
        <u-button 
          size="mini" 
          :type="filterType === 'nearby' ? 'primary' : 'info'"
          @click="setFilterType('nearby')"
        >
          附近
        </u-button>
      </view>
    </view>

    <!-- 停车场列表 -->
    <view class="parking-lot-list">
      <view 
        class="parking-lot-item" 
        v-for="lot in filteredParkingLots" 
        :key="lot.id"
        @click="handleSelectLot(lot)"
      >
        <view class="lot-header">
          <view class="lot-name">{{ lot.name }}</view>
          <view class="lot-status" :class="lot.statusClass">
            {{ lot.statusText }}
          </view>
        </view>
        
        <view class="lot-info">
          <view class="info-row">
            <u-icon name="map-pin" size="16" color="#666"></u-icon>
            <text class="info-text">{{ lot.address }}</text>
          </view>
          <view class="info-row">
            <u-icon name="car" size="16" color="#666"></u-icon>
            <text class="info-text">车位：{{ lot.availableSpaces }}/{{ lot.totalSpaces }}</text>
          </view>
          <view class="info-row">
            <u-icon name="rmb-circle" size="16" color="#666"></u-icon>
            <text class="info-text">{{ lot.pricePerHour }}元/小时</text>
          </view>
        </view>
        
        <view class="lot-actions">
          <u-button 
            size="mini" 
            type="primary" 
            @click.stop="handleNavigate(lot)"
          >
            导航
          </u-button>
          <u-button 
            size="mini" 
            type="success" 
            @click.stop="handleReserve(lot)"
            :disabled="lot.availableSpaces === 0"
          >
            预约
          </u-button>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view v-if="filteredParkingLots.length === 0" class="empty-state">
      <u-empty 
        text="暂无符合条件的停车场"
        mode="search"
      />
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'

// 搜索关键词
const searchKeyword = ref('')

// 筛选类型
const filterType = ref('all')

// 停车场数据
const parkingLots = ref([
  {
    id: 1,
    name: 'A区地下停车场',
    address: 'A区1号楼地下1层',
    totalSpaces: 120,
    availableSpaces: 35,
    pricePerHour: 5,
    status: 'available',
    statusText: '有空位',
    statusClass: 'status-available',
    distance: 0.2
  },
  {
    id: 2,
    name: 'B区地面停车场',
    address: 'B区2号楼前',
    totalSpaces: 80,
    availableSpaces: 0,
    pricePerHour: 3,
    status: 'full',
    statusText: '已满',
    statusClass: 'status-full',
    distance: 0.5
  },
  {
    id: 3,
    name: 'C区智能停车场',
    address: 'C区3号楼旁',
    totalSpaces: 200,
    availableSpaces: 68,
    pricePerHour: 6,
    status: 'available',
    statusText: '有空位',
    statusClass: 'status-available',
    distance: 0.8
  }
])

// 计算属性
const filteredParkingLots = computed(() => {
  let filtered = parkingLots.value

  // 关键词搜索
  if (searchKeyword.value) {
    filtered = filtered.filter(lot => 
      lot.name.includes(searchKeyword.value) || 
      lot.address.includes(searchKeyword.value)
    )
  }

  // 类型筛选
  switch (filterType.value) {
    case 'available':
      filtered = filtered.filter(lot => lot.availableSpaces > 0)
      break
    case 'nearby':
      filtered = filtered.filter(lot => lot.distance <= 0.5)
      break
    default:
      break
  }

  return filtered
})

// 搜索处理
const handleSearch = () => {
  console.log('搜索关键词:', searchKeyword.value)
}

// 设置筛选类型
const setFilterType = (type) => {
  filterType.value = type
}

// 选择停车场
const handleSelectLot = (lot) => {
  console.log('选择停车场:', lot)
}

// 导航到停车场
const handleNavigate = (lot) => {
  console.log('导航到停车场:', lot)
  uni.showToast({
    title: '正在打开地图导航',
    icon: 'success'
  })
}

// 预约车位
const handleReserve = (lot) => {
  if (lot.availableSpaces === 0) {
    uni.showToast({
      title: '该停车场暂无空位',
      icon: 'none'
    })
    return
  }
  
  uni.showToast({
    title: '预约功能开发中',
    icon: 'none'
  })
}

// 页面加载
onMounted(() => {
  loadParkingLots()
})

// 加载停车场数据
const loadParkingLots = async () => {
  try {
    console.log('加载停车场数据')
  } catch (error) {
    console.error('加载停车场数据失败:', error)
  }
}
</script>

<style lang="scss" scoped>
.space-query-page {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 16px;
  width: 100%;
  box-sizing: border-box;
}

.search-section {
  margin-bottom: 16px;
}

.filter-section {
  margin-bottom: 16px;
  
  .filter-row {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
  }
}

.parking-lot-list {
  .parking-lot-item {
    background: #fff;
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 16px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    
    .lot-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 12px;
      
      .lot-name {
        font-size: 18px;
        font-weight: 600;
        color: #333;
      }
      
      .lot-status {
        padding: 4px 8px;
        border-radius: 12px;
        font-size: 12px;
        font-weight: 500;
        
        &.status-available {
          background: #e8f5e8;
          color: #52c41a;
        }
        
        &.status-full {
          background: #fff2f0;
          color: #ff4d4f;
        }
      }
    }
    
    .lot-info {
      margin-bottom: 16px;
      
      .info-row {
        display: flex;
        align-items: center;
        margin-bottom: 8px;
        
        .info-text {
          margin-left: 8px;
          color: #666;
          font-size: 14px;
        }
      }
    }
    
    .lot-actions {
      display: flex;
      gap: 12px;
      justify-content: flex-end;
    }
  }
}

.empty-state {
  padding: 60px 0;
  text-align: center;
}
</style>
